<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://code.google.com/p/granizo" prefix="granizo"%>
<%@page import="yana.tpe.onlineshop.domain.domains.*" %>

<%if (pageContext.getAttribute("username") == null){
    Object principal = org.springframework.security.core.context.SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    if (principal instanceof org.springframework.security.core.userdetails.UserDetails) {
        pageContext.setAttribute("username", ((org.springframework.security.core.userdetails.UserDetails)principal).getUsername());
    } else {
        pageContext.setAttribute("username", principal.toString());
    }
}%>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <link href="<c:url value="/"/>style.css" rel="stylesheet" type="text/css" />
  </head>
  <body>
    <div id="container">
      <!-- header -->
      <div id="header">
        <div style="color:white; float:right; background:url(images/menu_enactive.gif)">
          <p><c:out value="${username}"/> | <a href='<c:url value="/"/>j_spring_security_logout'>Logout</a></p>
        </div>
        <div id="logo"><a href='<c:url value="/"/>'><span class="orange">Yana</span> online shop</a></div>
        <div id="menu">
          <ul>
            <li><a href='<c:url value="/"/>' >home</a></li>
            <li><a href='<c:url value="/admin"/>' class="active">adimn</a></li>
          </ul>
        </div>
      </div>
      <!--end header -->
      <!-- main -->
      <div id="main">
        <div id="content">
          <div id="text">
            <c:if test="${_type == 'category'}">
              <c:if test="${category_id != 'root'}">

                <fieldset>
                  <legend>Update current category</legend>
                  <form action='<c:url value="/admin/category/${category_id}/update"/>'
                        method="PUT">
                    <table>
                      <tr>
                        <td>Name</td>
                        <td><input name="name" value='<c:out escapeXml="true" value="${category.name}"/>'</td>
                        <td><input type="submit"/></td>
                      </tr>
                    </table>
                  </form>
                </fieldset>
              </c:if>

              <fieldset>
                <legend>Child categories</legend>
                <table border="0">
                  <c:forEach items="${categories}" var="c">
                    <tr>
                      <td>
                        <a href='<c:url value="/admin/category/${c.id}"/>'>
                          <c:out escapeXml="true" value="${c.name}"/>
                        </a>
                      </td>
                      <td>
                        <form action='<c:url value="/admin/category/${category_id}/delete/${c.id}"/>' method="DELETE">
                          <input type="submit" value="Delete"/>
                        </form>
                      </td>
                    </tr>
                  </c:forEach>
                </table>
                <fieldset>
                  <legend>Add new category</legend>
                  <form action='<c:url value="/admin/category/${category_id}/add"/>'
                        method="POST">
                    <table>
                      <tr>
                        <td><input name="name"/></td>
                        <td><input type="submit"/></td>
                      </tr>
                    </table>
                  </form>
                </fieldset>
              </fieldset>

              <fieldset>
                <legend>Products</legend>
                <table>
                  <c:forEach items="${products}" var="p">
                    <tr>
                      <td valign="top">
                        <a href='<c:url value="/admin/category/${p.category.id}/product/${p.id}"/>'>
                          <c:out escapeXml="true" value="${p.name}"/>
                        </a>
                      </td>
                      <td valign="top"><c:out value="${p.price}"/></td>
                      <td valign="top"><c:out escapeXml="true" value="${p.description}"/></td>
                      <td>
                        <form action='<c:url value="/admin/category/${category_id}/product/delete/${p.id}"/>'
                              method="DELETE">
                          <input type="submit" value="Delete"/>
                        </form>
                      </td>
                    </tr>
                  </c:forEach>
                </table>
                <c:if test="${category_id != 'root'}">
                <fieldset>
                  <legend>Add new product</legend>
                  <form action='<c:url value="/admin/category/${category_id}/product/add"/>'
                        method="POST" enctype="multipart/form-data">
                    <table>
                      <tr>
                        <td>Name</td>
                        <td><input name="name"/></td>
                      </tr>
                      <tr>
                        <td>Price</td>
                        <td><input name="price"/></td>
                      </tr>
                      <tr>
                        <td>Description</td>
                        <td><input name="description" type="text"/></td>
                      </tr>
                      <tr>
                        <td>Image</td>
                        <td><input name="image" type="file"/></td>
                      </tr>
                      <tr>
                        <td><input type="submit"/></td>
                      </tr>
                    </table>
                  </form>
                </fieldset>
                </c:if>
              </fieldset>
            </c:if>

            <c:if test="${_type == 'product'}">
                <table border="0">
                  <tr>
                    <c:set var="wasNavigated" value="false"/>
                    <c:forEach items="${categoriesNavigation}" var="c">
                      <c:if test='${wasNavigated}'>
                        <td> > </td>
                      </c:if>
                      <td>
                        <a href='<c:url value="/admin/category/${c.id}"/>'>
                          <c:out escapeXml="true" value="${c.name}"/>
                          <c:set var="wasNavigated" value="true"/>
                        </a>
                      </td>
                    </c:forEach>
                  </tr>
                </table>
              <form action='<c:url value="/admin/product/${product.id}"/>'
                    method="POST" enctype="multipart/form-data">
                <table>
                  <tr>
                  <td><input name="categoryId" value="${category_id}" type="hidden"/></td>
                  </tr>
                  <tr>
                    <td>Name</td>
                    <td><input name="name" value="${product.name}"/></td>
                  </tr>
                  <tr>
                    <td>Price</td>
                    <td><input name="price" type="number" step="0.1" value="${product.price}" min="0.1"/></td>
                  </tr>
                  <tr>
                    <td colspan="2">
                      <fieldset>
                        <legend>Description</legend>
                        <textarea name="description" value="${product.description}" />${product.description}</textarea>
                      </fieldset>
                    </td>
                  </tr>
                  <tr>
                    <td><img src='<c:url value="/product/${product.id}/image"/>' width="100" height="100"/></td>
                    <td valign="bottom"><input name="image" type="file"/></td>
                  </tr>
                  <tr>
                    <td><input type="submit"/></td>
                  </tr>
                </table>
              </form>
            </c:if>

          </div>
          <div id="sidebar">
            <h2>Categories</h2>
            <ul>
              <c:set var="previousLevel" value="0"/>
              <c:if test="${not empty categoriesTree}">
                <granizo:tree tree="${categoriesTree}" connectMethod="getChilds" var="status">
                  <granizo:startElement>
                    <c:if test="${previousLevel < status.level}">
                      <ul>
                      </c:if>
                      <li>
                        <c:set var="showLink" value="${status.element.id != category.id || _type != 'category'}"/>
                        <c:if test="${showLink}">
                          <a href='<c:url value="/admin/category/${status.element.id}"/>'>
                          </c:if>
                          ${status.element.name}
                          <c:if test="${showLink}">
                          </a>
                        </c:if>
                        </granizo:startElement>

                      <granizo:endElement>
                        <c:if test="${previousLevel > status.level}">
                      </ul>
                    </c:if>
                    </li>
                    </granizo:endElement>

                    <c:set var="previousLevel" value="${status.level}"/>
                </granizo:tree>
              </c:if>
            </ul>
          </div>
        </div>
      </div>
      <!-- end main -->

      <!-- footer -->
      <div id="footer">
        <div id="left_footer">&copy; Copyright 2013 Yana Potochniak
        </div>
        <div id="right_footer">

          <!-- Please do not change or delete this link. Read the license! Thanks. :-) -->
          Design by <a href="http://www.realitysoftware.ca" title="Website Design">Reality Software</a>

        </div>
      </div>
      <div style="display:none;"><a href="http://free-templates.ru">free-templates.ru</a></div>
      <!-- end footer -->
    </div>
  </body>
</html>
